import { AsyncStorage } from 'react-native';
import { kAuthorization, kContentType } from "../constant";
import Request from "../utils/Request";
const { Toast } = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_user";
export default {
    namespace: 'user',
    state: {
        loading: false,
        follow: {
            followers: 0,
            following: 0,
            isFollow: false,
            isMutual: false
        },
        colleague: [],
        info: null,
        hobbyList: null,
        skillList: null,
        hotMembers: []
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *follow({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            payload['params'] = Object.assign({}, payload);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            payload[kContentType] = 'application/json';
            const follow = yield call(Request.post, Request.getApi().follow, payload);
            if (follow) {
                yield put({ type: 'isFollow', payload: payload['params'] });
            }
        },
        *isFollow({ payload }, { call, put, select }) {
            let follow = { followers: 0, following: 0, isFollow: false, isMutual: false };
            yield put({ type: 'save', payload: { follow } });
            const auth = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            follow = yield call(Request.get, Request.getApi().isFollow, payload);
            if (follow) {
                yield put({ type: 'save', payload: { follow } });
            }
        },
        *fetchColleague({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            let p = {};
            p[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const colleague = yield call(Request.get, Request.getApi().colleague + `/${payload.uid}/${payload.cid}`, p);
            if (colleague) {
                if (Array.isArray(colleague)) {
                    yield put({ type: 'save', payload: { colleague } });
                }
            }
        },
        *fetchUserInfo({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            yield put({ type: 'save', payload: { info: null, loading: true } });
            const result = yield call(Request.get, Request.getApi().userInfoWithId + `/${payload.id}`, payload);
            yield put({ type: 'save', payload: { loading: false } });
            if (result && typeof result !== 'boolean') {
                yield put({
                    type: 'save',
                    payload: {
                        info: result
                    }
                });
            }
        },
        *fetchTags({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            const user = yield select(state => state.user);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const type = payload.type;
            const result = yield call(Request.get, Request.getApi().tags + `/${payload.type}`, payload);
            if (result) {
                if (type === 1) {
                    user.skillList = result;
                }
                else if (type === 2) {
                    user.hobbyList = result;
                }
                yield put({
                    type: 'save',
                    payload: Object.assign({}, user)
                });
            }
        },
        *fetchHotMembers({ payload }, { call, put, select }) {
            if (!payload)
                payload = {};
            const auth = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const hotMembers = yield call(Request.get, Request.getApi().hotMembers, payload);
            if (hotMembers) {
                yield put({
                    type: 'save',
                    payload: { hotMembers }
                });
            }
        },
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
